package cn.topkinson.es.etl;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.CommentGeneratorConfiguration;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.PluginConfiguration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author 黄子濠
 * @date 2020/5/29 10:43
 */
public class MybatisGeneratorMain {


    private static final String[] xmlPaths = {
        "src\\main\\resources\\generatorConfig.xml"
    };

    public static void main(String[] args) {
        try {
            MybatisGeneratorMain generatorSqlmap = new MybatisGeneratorMain();
            for (String xmlPath : xmlPaths) {
                System.out.println("==========开始反向生成：【"+xmlPath+"】==========");
                generatorSqlmap.generatorByPath(xmlPath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void generatorByPath(String xmlPath) throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File(xmlPath);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        List<Context> contexts = config.getContexts();
        for (Context context : contexts) {
            //PluginConfiguration plugin = new PluginConfiguration();
            //plugin.setConfigurationType("org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin");
            //context.addPluginConfiguration(plugin);
            //自定义注释生成器
            //CommentGeneratorConfiguration cm = context.getCommentGeneratorConfiguration();
            //cm.setConfigurationType("com.vzan.bigdata.model.minisns.MyCommentGenerator");
            //添加自动生成注释
            //CommentGenerator commentGenerator = context.getCommentGenerator();
            //Properties properties = new Properties();
            //properties.setProperty("suppressAllComments","false");
            //commentGenerator.addConfigurationProperties(properties);
        }
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }

}
